<?php

namespace app\common\model\Business;

use think\Env;
use think\Model;
use traits\model\SoftDelete;
// 个人账号表
class Business extends Model
{
    //模型对应的是哪张表
    protected $name = "business";
    // 开启自动写入时间戳字段 默认时间类型是int，如果是datetime的，那么需要把true改为datetime
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名 默认创建时间字段为create_time，更新时间字段为update_time
    protected $createTime = 'createtime';
    use SoftDelete;
    protected $deleteTime = 'deletetime';
    // 如果不需要更新时间把该属性设置为false
    protected $updateTime = false;
    // 追加数据表不存在的字段
    protected $append = [
        'avatar_text',
        'gender_text',
        'deal_text',
        'auth_text',
        'avatar_url',
        'region_text'
    ];
    /**
     * 
     * 定义头像默认值
     * @param $value 当前字段的值
     * @param $data 当前的所有数据数组
     */
    public function getAvatarTextAttr($value, $data)
    {
        $avatar = !empty($data['avatar']) ? $data['avatar'] : '/assets/home/images/avatar.jpg';
        return $avatar;
    }
    public function getRegionTextAttr($value, $data)
    {
        if (empty($data['id'])) {
            return '';
        }
        $province = model('Region.Region')->where(['code' => $data['province']])->find();

        $city = model('Region.Region')->where(['code' => $data['city']])->find();

        $district = model('Region.Region')->where(['code' => $data['district']])->find();

        $output = [];

        if ($province) {
            $output[] = $province['name'];
        }

        if ($city) {
            $output[] = $city['name'];
        }

        if ($district) {
            $output[] = $district['name'];
        }

        //广东省-广州市-海珠区
        return implode('-', $output);
    }
    public function getAvatarUrlAttr($value, $data)
    {
        $url = Env::get('site.url', config('site.url'));
        $avatar = !empty($data['avatar']) ? $data['avatar'] : '/assets/home/images/avatar.jpg';
        return $url . $avatar;
    }
    public function getGenderTextAttr($value, $data)
    {
        if (empty($data['id'])) {
            return '';
        }
        $Gender = ['0' => '保密', '1' => '男', '2' => '女'];
        if (!empty($data['gender']) || $data['gender'] == 0) {
            return $Gender[$data['gender']];
        }
        exit;
        return '';
    }
    public function getDealTextAttr($value, $data)
    {
        if (empty($data['id'])) {
            return '';
        }
        $Deal = ['0' => '未成交', '1' => '已成交'];
        if (!empty($data['deal']) || $data['deal'] == 0) {
            return $Deal[$data['deal']];
        }
        return '';
    }
    public function getAuthTextAttr($value, $data)
    {
        if (empty($data['id'])) {
            return '';
        }
        $auth = ['0' => '未认证', '1' => '已认证'];
        if (!empty($data['auth']) || $data['auth'] == 0) {
            return $auth[$data['auth']];
        }
        return '';
    }

    // 关联查询（根据业务需求）
    public function scource()
    {
        return $this->belongsTo('app\common\model\Business\Source', 'sourceid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function admin()
    {
        return $this->belongsTo('app\common\model\Admin','adminid','id',[],'LEFT')->setEagerlyType(0);
    }
    public function district()
    {
        return $this->belongsTo('app\common\model\Region\Region', 'district', 'code', [], 'LEFT')->setEagerlyType(0);
    }
    public function city()
    {
        return $this->belongsTo('app\common\model\Region\Region', 'city', 'code', [], 'LEFT')->setEagerlyType(0);
    }
    public function province()
    {
        return $this->belongsTo('app\common\model\Region\Region', 'province', 'code', [], 'LEFT')->setEagerlyType(0);
    }
}
